package com.cn.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.concurrent.Executors;

/**
 * 配置线程池启动定时任务
 */
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
        scheduledTaskRegistrar.addTriggerTask(() -> {
            System.out.println("执行动态定时任务: ");
        }, triggerContext -> {
            String cron = "*/5 * * * * ?";
            // 返回执行周期
            return new CronTrigger(cron).nextExecutionTime(triggerContext);
        });
    }
}
